package org.jeecg.modules.yw.mapper;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.jeecg.modules.yw.entity.Appointment;
import org.jeecg.modules.yw.entity.Order;
import org.jeecg.modules.yw.vo.OrderQueryVO;
import org.jeecg.modules.yw.vo.OrderVo;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

@Mapper
public interface OrderMapper extends BaseMapper<Order> {
	
    @Insert("INSERT INTO `order` (id, orderNo, ownerName, ownerPhone, ownerAddress, fl, cc,ccNum, ccfs,ccfsNum, cclx,wgsc,wgscNum, cpby, yscdlx, sghj, sfjj, jjlx, yyTime, currentUserId,cshs,cshsContent,dkfc,dkfcContent, `status`, delFlag, state, createUserId, createTime, areas) "
    		+ "VALUES(#{id}, #{orderNo}, #{ownerName}, #{ownerPhone},#{ownerAddress},#{fl}, #{cc},#{ccNum}, #{ccfs}, #{ccfsNum},#{cclx},#{wgsc},#{wgscNum}, #{cpby},#{yscdlx}, #{sghj},#{sfjj}, #{jjlx},#{yyTime},#{currentUserId},#{cshs},#{cshsContent},#{dkfc},#{dkfcContent},#{status},#{delFlag},#{state},#{createUserId}, #{createTime}, #{areas})")
    public int inserOrder(Order order);
        
    
    @Select("SELECT orderNo FROM `order` where DATE_FORMAT(createTime, '%Y-%m-%d') = #{today} order by createTime desc limit 0,1")
    public String createNewBidNumber(@Param("today") String today);
    
    
    @Select("SELECT orderNo FROM `order` where (currentUserId = #{currentUserId} and state = 0) or (mcschsState = 1) or (mcdkfcState = 1)")
    public List<Order> getOrderList(@Param("currentUserId") String currentUserId);
    
    /**
     * 得到组员的list
     * @param appointId
     * @return
     */
    @Select("SELECT * FROM `order` where id in (select orderId from appointment where appointId = #{currentUserId} and appointType = 0) order by createTime desc")
    public List<Order> getZyOrderList(@Param("currentUserId") String currentUserId);
    
    
    @Select("SELECT id,orderNo,ownerName,ownerPhone,ownerAddress,fl,cc,ccNum,ccfs,ccfsNum,cclx,wgsc,wgscNum,cpby,yscdlx,sghj,sfjj,jjlx,yyTime,currentUserId,cshs,cshsContent,dkfc,dkfcContent,gzsh1,gzsh2,nwlhs,mshs,blps,pjwr,kqsflw,zskg,fpbm,ljql,wjq,njq,wblj,nblj,status,orderStatus,azsgStatus,djshStatus,delFlag,state,createUserId,createTime,mcschsState,mcdkfcState,zkschsState,zkdkfcState,updateTime,azbzUserId,djshUserId,azbzReason,djshReason,djTime,jsTime,createTime,jsPath,jsReason,djFinishTime,final_price finalPrice,finishTime FROM `order` where id = #{id}")
    public Order getOrder(String id);
    @Select("SELECT * FROM `order` where orderNo = #{orderNo}")
    public Order getOrderByOrderNo(@Param("orderNo") String orderNo);
    
    
    @Update({
        "<script> ",
        "update `order` set ",
        "<if test = \"orderStatus != null\"> ",
        "orderStatus=#{orderStatus}, ",
        "</if> ",
        "<if test = \"azsgStatus != null\"> ",
        "azsgStatus=#{azsgStatus}, ",
        "</if> ",      
        "<if test = \"djshStatus != null\"> ",
        "djshStatus=#{djshStatus}, ",
        "</if> updateTime = now()",  
        " WHERE id=#{orderId}",
        "</script>"
        })
      int updateStatus(
                    @Param("orderStatus") Integer orderStatus,
                    @Param("azsgStatus") Integer azsgStatus,
                    @Param("djshStatus") Integer djshStatus,
                    @Param("orderId") String orderId
                    );
    
    
    @Update({
        "<script> ",
        "update `order` set ",
        "<if test = \"cshs != null\"> ",
        "cshs=#{cshs}, ",
        "</if> ",
         "<if test = \"cshsContent != null\"> ",
         "cshsContent=#{cshsContent}, ",
         "</if> ",
        "<if test = \"dkfc != null\"> ",
        "dkfc=#{dkfc}, ",
        "</if> ",
         "<if test = \"dkfcContent != null\"> ",
         "dkfcContent=#{dkfcContent}, ",
         "</if> ",
         "<if test = \"gzsh1 != null\"> ",
        "gzsh1=#{gzsh1}, ",
        "</if> ",  
        "<if test = \"gzsh2 != null\"> ",
        "gzsh2=#{gzsh2}, ",
        "</if> ",  
        "<if test = \"nwlhs != null\"> ",
        "nwlhs=#{nwlhs}, ",
        "</if> ",  
        "<if test = \"mshs != null\"> ",
        "mshs=#{mshs}, ",
        "</if> ", 
        "<if test = \"blps != null\"> ",
        "blps=#{blps}, ",
        "</if> ",      
        "<if test = \"pjwr != null\"> ",
        "pjwr=#{pjwr}, ",
        "</if> ",         
        "<if test = \"kqsflw != null\"> ",
        "kqsflw=#{kqsflw}, ",
        "</if> ",    
        "<if test = \"zskg != null\"> ",
        "zskg=#{zskg}, ",
        "</if> ",   
        "<if test = \"fpbm != null\"> ",
        "fpbm=#{fpbm}, ",
        "</if> ",      
        "<if test = \"ljql != null\"> ",
        "ljql=#{ljql}, ",
        "</if> ",    
        "<if test = \"wjq != null\"> ",
        "wjq=#{wjq}, ",
        "</if> ",      
        "<if test = \"njq != null\"> ",
        "njq=#{njq}, ",
        "</if> ",  
        "<if test = \"wblj != null\"> ",
        "wblj=#{wblj}, ",
        "</if> ",  
        "<if test = \"nblj != null\"> ",
        "nblj=#{nblj}, ",
        "</if> ",    
        "<if test = \"status != null\"> ",
        "`status`=#{status}, ",
        "</if> ",     
        "<if test = \"djTime != null\"> ",
        "djTime=#{djTime}, ",
        "</if> ",   
        "<if test = \"djFinishTime != null\"> ",
        "djFinishTime=#{djFinishTime}, ",
        "</if> ",    
        "<if test = \"jsPath != null\"> ",
        "jsPath=#{jsPath}, ",
        "</if> ",         
        "<if test = \"orderStatus != null\"> ",
        "`orderStatus`=#{orderStatus}, ",
        "</if> ",  
        "<if test = \"azsgStatus != null\"> ",
        "`azsgStatus`=#{azsgStatus}, ",
        "</if> ", 
        "<if test = \"djshStatus != null\"> ",
        "`djshStatus`=#{djshStatus}, ",
        "</if> ",         
        "<if test = \"state != null\"> ",
        "`state`=#{state}, ",
        "</if> ",   
        "<if test = \"mcschsState != null\"> ",
        "mcschsState=#{mcschsState}, ",
        "</if> ",    
        "<if test = \"mcdkfcState != null\"> ",
        "mcdkfcState=#{mcdkfcState}, ",
        "</if> ",   
        "<if test = \"zkschsState != null\"> ",
        "zkschsState=#{zkschsState}, ",
        "</if> ", 
        "<if test = \"zkdkfcState != null\"> ",
        "zkdkfcState=#{zkdkfcState}, ",
        "</if> ", 
        "<if test = \"currentUserId != null\"> ",
        "currentUserId=#{currentUserId}, ",
        "</if> ", 
        "<if test = \"azbzUserId != null\"> ",
        "azbzUserId=#{azbzUserId}, ",
        "</if> ", 
        "<if test = \"djshUserId != null\"> ",
        "djshUserId=#{djshUserId}, ",
        "</if> ",   
        "<if test = \"azbzReason != null\"> ",
        "azbzReason=#{azbzReason}, ",
        "</if> ", 
        "<if test = \"jsReason != null\"> ",
        "jsReason=#{jsReason}, ",
        "</if> ", 
        "<if test = \"jstime != null\"> ",
        "jsTime=#{jstime}, ",
        "</if> ",
        "<if test = \"finalPrice != null\"> ",
        "final_price=#{finalPrice}, ",
        "</if> ",
        "<if test = \"finishTime != null\"> ",
        "finishTime=#{finishTime}, ",
        "</if> ",
        "<if test = \"ypzzbz != null\"> ",
        "ypzzbz=#{ypzzbz}, ",
        "</if> ",      
        "<if test = \"ypdjsh != null\"> ",
        "ypdjsh=#{ypdjsh}, ",
        "</if> ",      
        "<if test = \"xgsj != null\"> ",
        "xgsj=#{xgsj}, ",
        "</if> ",  
        "<if test = \"teamRead != null\"> ",
        "team_read=#{teamRead}, ",
        "</if> ",        
        "<if test = \"djshReason != null\"> ",
        "djshReason=#{djshReason}, ",
        "</if>  updateTime = now() ",   
        " WHERE id=#{id}",
        "</script>"
        })
      int updateOrderInfo(Order order);
    
	/**
	 * 得到订单
	 */
     public IPage<OrderQueryVO> getAllOrders(Page<OrderQueryVO> page, @Param(Constants.WRAPPER) QueryWrapper<OrderQueryVO> queryWrapper);
    
     
 	/**
 	 * 得到消息
 	 */
      IPage<Order> getOrdersMsg(Page<Order> page, @Param(Constants.WRAPPER) QueryWrapper<Order> queryWrapper);

     IPage<Order> getOrderMsgs(Page<Order> page, @Param(Constants.WRAPPER) QueryWrapper<Order> queryWrapper);

    IPage<OrderVo> getPage(IPage<OrderVo> page, @Param("start") Date start, @Param("end") Date end,
                           @Param("orderStatus") String orderStatus, @Param("orderno") String orderno,
                           @Param("cardid") String cardid, @Param("problem") String problem,
                           @Param("area") String[] areas,@Param("enters") String[] enters,@Param("name") String name,@Param("ownerPhone") String ownerPhone);

    /*
    * 查询不同角色 不同状态的订单
    * */
    @Select("SELECT * FROM `order` WHERE " +
            "((`azbzUserId` = #{id} AND `yyTime` >= #{startTime} AND `yyTime` < #{endTime}) " +
            "OR (`djshUserId` = #{id} AND `djTime` >= #{startTime} AND `djTime` < #{endTime}))")
    List<Order> selectOrdersByCondition(@Param("id") String id,@Param("startTime") String startTime,@Param("endTime")  String endTime);

    OrderVo getByVoId(String id);
    
    
    
    @Update("update `order` set ypdjsh = #{ypdjsh} where id = #{orderId}")
    public int updateDjsh(@Param("ypdjsh") String ypdjsh, @Param("orderId")  String orderId);  
    
    
    @Update("update `order` set ypzzbz = #{ypzzbz} where id = #{orderId}")
    public int updateAzbz(@Param("ypzzbz") String ypzzbz, @Param("orderId")  String orderId);   
    
    
    
    /*
    * 查询不同时间段的订单
    * */
    @Select("SELECT createUserId, round(sum(final_price),2) finalPrice FROM `order` WHERE orderStatus = 10 and final_price is not null  AND `xgsj` >= #{startTime} AND `xgsj` <= #{endTime}  group by createUserId ")
    List<Order> selectPaymentOrder(@Param("startTime") String startTime,@Param("endTime")  String endTime);
    
    
    
    /*
     * 查询不同时间段的订单
     * */
     @Select("SELECT xgsj FROM `order` WHERE orderStatus = 10 and final_price is not null and createUserId = #{createUserId} and `xgsj` >= #{startTime} AND `xgsj` <= #{endTime}  order by xgsj desc limit 0,1 ")
     String getJsTime(@Param("createUserId") String createUserId,@Param("startTime") String startTime, @Param("endTime")  String endTime);
    
}
